package com.dui.testbarchart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class Bar extends View {
    private int mBarColor = Color.argb(255, 0x33, 0x33, 0x99);
    private int mDescriptionColor = Color.argb(255, 0xff, 0xcc, 0x33);
    private int mValueColor = Color.argb(255, 0xff, 0x00, 0x33);
    private int mValueBgColor = Color.argb(255, 0x66, 0x33, 0x99);

    private int mBarWidth= 100;
    private int mDescriptionTextSize = 20;
    private int mValueTextSize= 20;


    private String mDescription = "888";
    private String mValue = "55";

    private Paint mBarPaint;
    private Paint mDescriptionPaint;
    private Paint mValuePaint;
    private Paint mValueBgPaint;


    private int mBarHeight;

    public Bar(Context context) {
        super(context);
        init(context, null);
    }

    public Bar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public Bar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public Bar(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mBarPaint = new Paint();
        mBarPaint.setAntiAlias(true);
        mBarPaint.setColor(mBarColor);
        mBarPaint.setStyle(Paint.Style.STROKE);
        mBarPaint.setStrokeWidth(mBarWidth);

        mDescriptionPaint = new Paint();
        mDescriptionPaint.setAntiAlias(true);
        mDescriptionPaint.setColor(mDescriptionColor);
        mDescriptionPaint.setStyle(Paint.Style.STROKE);
        mDescriptionPaint.setTextSize(mDescriptionTextSize);

        mValuePaint = new Paint();
        mValuePaint.setAntiAlias(true);
        mValuePaint.setColor(mValueColor);
        mValuePaint.setStyle(Paint.Style.STROKE);
        mValuePaint.setTextSize(mValueTextSize);

        mValueBgPaint = new Paint();
        mValueBgPaint.setAntiAlias(true);
        mValueBgPaint.setColor(mValueBgColor);
        mValueBgPaint.setStyle(Paint.Style.STROKE);
        mValueBgPaint.setStrokeWidth(1);

        mBarHeight = 128;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //数值柱子
        canvas.drawLine(0,getHeight()-mDescriptionTextSize,0,(getHeight()-mDescriptionTextSize-mBarHeight), mBarPaint);

        //底部描述Text
        canvas.drawText(mDescription,(mBarWidth/4-mDescriptionPaint.measureText(mDescription)/2),getHeight(), mDescriptionPaint);

        //顶部数值Text
        canvas.drawText(mValue,(mBarWidth/4-mValuePaint.measureText(mValue)/2),(getHeight()-mDescriptionTextSize-mBarHeight-18), mValuePaint);

        //为顶部数值画一个圆角框 三角尖头
        RectF valuebgRect = new RectF(5,(getHeight()-mDescriptionTextSize-mBarHeight-20-mValueTextSize),(mBarWidth/2-5),(getHeight()-mDescriptionTextSize-mBarHeight-10));
        canvas.drawRoundRect(valuebgRect, 10, 10, mValueBgPaint);
        //三角
        Path path3 = new Path();
        path3.moveTo((valuebgRect.centerX()-5), valuebgRect.bottom);
        path3.lineTo(valuebgRect.centerX(), valuebgRect.bottom+5);
        path3.lineTo((valuebgRect.centerX()+5), valuebgRect.bottom);
        path3.close();
        mValueBgPaint.setStyle(Paint.Style.FILL);
        canvas.drawPath(path3, mValueBgPaint);

    }
}